package org.example.booksmanagementsystem.service;

import lombok.extern.slf4j.Slf4j;
import org.example.booksmanagementsystem.enums.BookInfoStatusEnum;
import org.example.booksmanagementsystem.enums.ResultStatus;
import org.example.booksmanagementsystem.model.*;
import org.example.booksmanagementsystem.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    //图书管理员验证
    public UserInfo queryUserInfoByUserName(String userName, String password){

        //判断账号密码是否正确
        UserInfo userInfo = userInfoMapper.queryUserInfoByUserName(userName);

        if(userInfo==null){//得不到用户信息
            return null;
        }else{ //得到用户信息
            if(password.equals(userInfo.getPassword())){
                userInfo.setUserType("librarian");
                return userInfo;
            }
            else{
                return null;
            }
        }

    }

    //普通用户验证
    public UserInfo queryUserInfoByUserNameNormal(String userName, String password){

        //判断账号密码是否正确
        UserInfo userInfo = userInfoMapper.queryUserInfoByUserNameNormal(userName);

        if(userInfo==null){//得不到用户信息
            return null;
        }else{ //得到用户信息
            if(password.equals(userInfo.getPassword())){
                userInfo.setUserType("normal");
                return userInfo;
            }
            else{
                return null;
            }
        }

    }


    @Transactional
    public Result registerUserInfo(UserInfo userInfo){
        /*
        1.查询注册用户是否存在
        2.如果用户不存在进行调用Mapper进行注册
        3.为注册成功的用户创建一个用户图书表
        * */

        Result result = new Result();

        //查询用户是否存在
        UserInfo getUserInfo = userInfoMapper.queryUserInfoByUserNameNormal(userInfo.getUserName());
        if(getUserInfo !=null){
            result.setErrorMessage("用户已存在，请重新注册");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        // 将要注册的用户不存在，进行注册用户
        Integer count = userInfoMapper.insertUserInfo(userInfo);
        if(count==1){
            result.setStatus(ResultStatus.SUCCESS);

            //查询新注册用户的ID
            UserInfo u = userInfoMapper.queryUserInfoByUserNameNormal(userInfo.getUserName());

            //根据用户id创建表--存储图书的表
            userInfoMapper.createBooktable(u.getId());

            //根据用户id创建表-- 存储特价专区的表
            userInfoMapper.createSpecialBooktable(u.getId());


            return result;
        }else{
            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("用户注册失败");
            log.info("新用户插入后返回的值不是1");
            return result;
        }


    }

    // 获取用户--分页
    @Transactional
    public PageResult<UserInfo> queryNormalUserInfoList(PageRequest pageRequest){

        if(pageRequest==null){
            return null;
        }

        //获取表中数据的总数
        Integer count = userInfoMapper.queryNormalUserInfoCount();

        //获取该页的数据
        List<UserInfo> userInfoList = userInfoMapper.queryNormalUserInfoList(pageRequest);

        //根据deleteFlag 修改 deleteStatus
        for(UserInfo it : userInfoList){
            if(it.getDeleteFlag()==0){
                it.setDeleteStatus("存在");
            }else{
                it.setDeleteStatus("删除");
            }
        }

        // 创建PageResult对象返回给Controller
        return new PageResult<UserInfo>(count,userInfoList,pageRequest);


    }

    public UserInfo getUserInfoById(Integer id){

        UserInfo userInfo = userInfoMapper.getUserInfoById(id);

        return userInfo;
    }

    public Integer updateNormalUserInfoPassword(UserInfo userInfo){

        Integer n = userInfoMapper.updateNormalUserInfoPassword(userInfo);

        return n;
    }

    //逻辑删除用户
    public Integer updateUserStatus(Integer id){
        Integer n =userInfoMapper.updateUserStatus(id);
        return n;
    }

    public Integer updateBatchUserStatus(List<Integer> idList){
        Integer n = userInfoMapper.updateBatchUserStatus(idList);

        return n;
    }
}

